package com.xxtx.trade.web;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.xxtx.trade.agent.dto.ImeiToPhoneDto;
import com.xxtx.trade.common.util.model.Result;
import com.xxtx.trade.sysUser.dto.TradeSysRoleDto;
import com.xxtx.trade.sysUser.dto.TradeSysUserDto;
import com.xxtx.trade.sysUser.service.TradeExportService;
import com.xxtx.trade.sysUser.vo.ImeiToPhoneVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;


@RestController
@RequestMapping("/api/v1/xxtx/excels")
public class ExcelController {
    @Autowired
    private TradeExportService tradeExportService;

    /**
     * 查询导出列表
     *
     * @param form
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/export/syncTaskList")
    public Result adminExportExcelList(@RequestBody TradeSysUserDto form, HttpServletRequest request, HttpServletResponse response) {
        return tradeExportService.selectDownLoadList(form);
    }


    //导出抖音数据
    @RequestMapping("/all/urlInfos")
    public Result allUrlInfos(@RequestBody TradeSysRoleDto form, HttpServletRequest request, HttpServletResponse response) {
        return tradeExportService.allUrlInfos(form);
    }

    //导出腾讯数据
    @RequestMapping("/all/tencentUrlInfos")
    public Result allTencentInfos(@RequestBody TradeSysRoleDto form, HttpServletRequest request, HttpServletResponse response) {
        return tradeExportService.allTencentInfos(form);
    }


    @RequestMapping("/all/baiduUrlInfos")
    public Result allBaiduInfos(@RequestBody TradeSysRoleDto form, HttpServletRequest request, HttpServletResponse response){
        return tradeExportService.allBaiduInfos(form);
    }

    @RequestMapping("/all/ksUrlInfos")
    public Result allKuaishouInfos(@RequestBody TradeSysRoleDto form, HttpServletRequest request, HttpServletResponse response){
        return tradeExportService.allKuaishouInfos(form);
    }

    @RequestMapping("/upload/imeiToPhone")
    public Result uploadImeiToPhone(MultipartFile file) {
        // 相关dto保存在sysUser下的vo中，ImeiToPhoneVO
        List<ImeiToPhoneVO> excelVOS = null;
        InputStream in = null;
        ImportParams params = new ImportParams();
        try {
            // 1、解析文件
            in = file.getInputStream();
            excelVOS = ExcelImportUtil.importExcel(in, ImeiToPhoneVO.class, params);
            return tradeExportService.uploadImeiToPhone(excelVOS);
        } catch (Exception e) {
            Result result = new Result();
            result.setCode(400);
            result.setMessage("上传错误，请稍后重试！");
            return result;
        }
        //2、校验文件格式和参数校验
        //3、组装数据库输入对象
        //4、插入数据库


    }

}
